//
//  ZDCommitListController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/16.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDCommitListController.h"
#import "ZDCommitModel.h"
#import "ZDCommitListCell.h"

static NSString *ZDCommitListCellId = @"ZDCommitListCell";

@interface ZDCommitListController ()

@end

@implementation ZDCommitListController

- (void)setupUI
{
    [super setupUI];
    
    self.title = @"评价信息";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDCommitListCell" bundle:nil] forCellReuseIdentifier:ZDCommitListCellId];
    [self initHeader];
    [self initFooter];
    
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
//    act=goods&op=goods_evaluate&curpage=1&goods_id=392&page=10&key=677e3d2374b67e5139fca5be70082508
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:self.goodsId forKey:@"goods_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:GoodsCommitListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            
            NSArray *tempt = datas[@"goodsevallist"];
            NSArray *tempModel = [ZDCommitModel mj_objectArrayWithKeyValuesArray:tempt];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDCommitListCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDCommitListCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

@end
